define(function(require, exports){
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var highGrid  = require('@base/highGrid');
	var grid = require('@base/grid');
	var util = require('util');


	// 用户-消息查看列表
	var MessageGrid = highGrid.base.extend({
		init: function(config){
			config = $.extend({
				'reqType': 'websocket',
				'url': 'usermessage/list',
				'cols': [
					{type: 'id', width: 60, align: 'center'},
					{name: 'Title', text:LANG('标题'), align:'left',render:'renderTitle',width:600}
					//{name:'From', text: LANG('发布人'), align: 'left'},
				],
				'metrics': [
					{name: 'UpdateTime', text:LANG('发布时间'),align:'center', render: 'formatDate'}
				],
				'isSendMetrics': false,
				'hasTab': false,
				'hasAmount': false,
				'hasSearch': false,
				'hasBatch': false,
				'hasExport': false,
				'hasSwitch': false,
				'hasMenu': true,
				'hasRefresh': true,
				'gridName': 'message'
			}, config);

			this.Super( "init", arguments );
		},
		renderId: function(idx, val, data, col, td){
			if(!data.IsRead){
				td.css({
					fontWeight: 'bold'
				})
			}else{
				td.css({
					fontWeight: 'normal'
				})
			}
			return val;
		},
		// 格式化时间
		formatDate: function(i, val, data, con) {
			return val?util.date('Y-m-d H:i:s',val):null;
		},
		renderTitle: function(i, val, data, con, td){
			if(!data.IsRead){
				td.css({
					fontWeight: 'bold'
				})
			}else{
				td.css({
					fontWeight: 'normal'
				})
			}
			var dom = $('<a class="M-tableListWidthLimit" />').attr({
				// 'target': '_blank',
				'href': '#user/message/check/' + data._id
			});
			return dom.text(val).width(con.width).attr('title',val);
		}
	});
	exports.messageGrid = MessageGrid;


	// 管理员-消息管理列表
	var MessageManageGrid = highGrid.base.extend({
		init: function(config) {
			config = $.extend(true, {
				'reqType': 'websocket',
				'url': 'message/list',
				'cols':[
					{type: 'id', width: 60, align: 'center'},
					{name:'Title', text: LANG('标题'), type: 'index', align: 'left', width: 500, render:'renderTitle'}
				],
				'metrics': [
					{name:'To', text: LANG('收件人'), align: 'left', render:'renderNames',width:200},
					{name:'From', text: LANG('发布人'), align: 'left'},
					{name:'UpdateTime', text: LANG('发布时间'), align: 'center', render:'formatDate'}
				],
				'hasTab': false,
				'hasAmount': false,
				'hasExport': false,
				'hasMenu': true,
				'hasSearch': false,
				'gridName': 'messageManage'
			}, config);
			this.Super( "init", arguments );
		},
		// 格式化时间
		formatDate: function(i, val, data, con) {
			return val?util.date('Y-m-d H:i:s',val):null;
		},
		renderTitle: function(i, val, data, con){
			var dom = $('<a class="M-tableListWidthLimit" />').attr({
				// 'target': '_blank',
				'href': '#admin/messageManage/check/' + data._id
			});
			return dom.text(val).width(con.width).attr('title',val);
		},
		renderNames: function(i, val, row, con) {
			if (!val || !val.length) {
				return LANG('所有人');
			}
			var result = [];
			for (var j = 0, len = row.Recipient.length; j < len; j++) {
				if (row.Recipient[j]) {
					result.push(row.Recipient[j]);
				}
			}
			var resultStr = result.join('、');
			return $('<div class="M-tableListWidthLimit" title="' + resultStr + '">').width(con.width).text(resultStr);
		}
	});
	exports.messageManageGrid = MessageManageGrid;


	// 选择客户列表
	var CustomerGrid = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
					'reqType': 'websocket',
					'url': 'user/list',
					'cols': [
						{name:"UserId",text: 'ID', type:'fixed',sort:false,align:'center', width:60},
						{name: "Name", text: LANG("用户名称"), type: "index", align: "left", width: 400, render: 'renderName'},
						//{name: "Name", text: LANG("邮箱"), align: "left", width: 250, sort: false},
						{name: "Type", text: LANG("类型"), align: "center", render: 'renderType', sort: false, width: 100},
						{name: "ExchangeInfo", text: LANG("版本"), align: "center", render: 'renderCategory', sort: false, width: 100}
					],
					'sub_field': 'UserId',	// 索引字段名称
					'hasAmount': false,		// 是否有总计模块
					'hasPager': true,		// 是否有分页模块
					'hasExport': false,		// 是否有导出模块
					'hasBatch': false,		// 是否有批量操作
					'hasTab': false,			// 是否显示指标分组模块
					'hasRefresh': false,		// 刷新控件
					'hasSearch': true,
					//'singleSelect': true,
					'pager':{
						"size":10,
						"bounds":5,
						"firstLast":false,
						"showJumper":0,
						"showSizeTypes":0
					},
					'hasSelect': true,
					'layout': {
						"rows":['search(.pb10)(auto)|filter(.h30 lh30 ml20)(auto)']
					},
					'list':{
						'rowSelect': true,
						'scroll_type': 'row',
						'scroll_size': 5
					},
					'sort': 'CreateTime'
				}
			);
			this.Super('init', arguments);
		},
		renderName: function(i, val, row, con){
			var html = $([
				'<div class="uk-text-truncate tl fl" title="'+(val||'')+'">',
					(val||''),
				'</div>'
			].join('')).width(con.width-20);
			return html;
		},
		// 根据type的数字生成对应类型名称
		renderType: function (i, val, row, con) {
			var result = '';
			switch (val) {
				case 2:
					result = LANG('直客');
					break;
				case 3:
					result = LANG('员工');
					break;
				case 4:
					result = LANG('管理员');
					break;
				default: // 1
					result = LANG('代理');
					break;
			}
			return result
		},
		// 根据CategoryId生成对应种类名称
		renderCategory: function (i, val, row, con) {
			var result = '';

			switch (val && val.CategoryId) {
				case 2:
					result = LANG('无产品版')
					break;
				default: // 1
					result = LANG('有产品版')
					break;
			}
			return result
		}
	});
	exports.customerGrid = CustomerGrid;

});